package fr.afcepf.ai90.qualimetrie.data;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import com.mysql.jdbc.log.Log;

import fr.afcepf.ai90.qualimetrie.entity.Commande;
import fr.afcepf.ai90.qualimetrie.entity.Personne;
import fr.afcepf.ai90.qualimetrie.util.SimpleDataSource;

public class DataCommandeImpl implements IDataCommande {

    
    private String requete="INSERT INTO commande(date_commande,id_personne) values(?,?)";
    
    
 
    @Override
    public Integer creationCommande(Commande cmd) {
        
        
        DataSource ds = new SimpleDataSource();
        Integer retour=null;
        
        
        try {
            Connection cnx = ds.getConnection();
            PreparedStatement pst = cnx.prepareStatement(requete,Statement.RETURN_GENERATED_KEYS);
            pst.setDate(1, new java.sql.Date(cmd.getDateCommande().getTime()));
            pst.setInt(2,cmd.getPersonne().getId());
            Integer rs = pst.executeUpdate();
            ResultSet rskey = pst.getGeneratedKeys();
            rskey.next();
            retour = rskey.getInt(1);
           
                       
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }        
        return retour;
                
    }

}
